# cython: language_level=3
#!/usr/bin/python
# -- coding:utf8 --

import app.model_vn.util_log as util_log
import pymysql
import pandas as pd

import warnings

warnings.filterwarnings("ignore")


def select_sql_mysql(sql):
    """
    风控库获取数据
    :param sql:
    :return:
    """
    conn = pymysql.Connect(
        host="159.138.99.18",
        port=3306,
        user="root",
        passwd="Qiuliu123!@#",
        database="adms_core_vn_dc",
    )
    df = pd.read_sql(sql, con=conn)
    conn.close()
    return df


def select_sql_mysql_v1(sql):
    """
    从业务库里面获取数据集
    :param sql:
    :return:
    """
    conn = pymysql.Connect(
        host='190.92.198.191',
        port=3306,
        user='readonly',
        passwd='Gyt@20231122',
        database='dcnew'
    )
    df = pd.read_sql(sql, con=conn)
    conn.close()
    return df


def get_is_none(x):
    """
    #判断参数是否为空
    Parameters
    ----------
    x：参数

    Returns
    -------
    返回布尔值
    """
    if x is None or str(x).replace(" ", "").lower().strip() in [
        "-999999",
        "-999976",
        "-999977",
        "-999978",
        "-999999.0",
        "-999976.0",
        "-999977.0",
        "-999978.0",
        "none",
        "nan",
        "nat",
        "",
        "000000000000000",
    ]:
        return True
    else:
        return False


def get_is_match(parameter1, parameter2):
    """
    判断是否完全匹配
    Parameters
    ----------
    parameter1：参数1
    parameter2：参数2

    Returns
    -------
    返回布尔值（1或者0）
    """
    try:
        if parameter1 == parameter2:
            return 1
        else:
            return 0
    except Exception as e:
        util_log.get_logger().error(e)
        return -9976

def process_phone_number(phone_number):
    """
    手机号码处理，用于识别存储的数据表
    :param phone_number:
    :return:
    """
    # 清除空格
    phone_number = phone_number.replace(" ", "")

    # 第一步，截取后两位
    last_two_digits = phone_number[-2:]

    # 第二步，若第二位为0，取第一位
    if last_two_digits[0] == '0':
        processed_number = last_two_digits[1]
    else:
        processed_number = last_two_digits

    # 第三步，将字符转换为整数
    processed_number = int(processed_number)

    return processed_number


# 把短信内容转化成可读取的内容格式
def decode_str(x):
    arr = x.split(',')
    content = ""
    if len(arr) > 0:
        try:
            for v in arr:
                vDecode = chr(int(v, 16))
                content = content + vDecode
        except UnicodeEncodeError:
            content = content.encode('utf-8', errors='ignore').decode('utf-8', errors='ignore')
            # 或者使用 content = content.encode('latin-1', errors='ignore').decode('latin-1', errors='ignore')
        except:
           # print(v)
            content = content + v
    else:
        content = ""
    return content